# Maintainer: Michael Hansen <zrax0111 gmail com>

_realname=capstone
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-capstone"
         "${MINGW_PACKAGE_PREFIX}-python-capstone")
_pkgver=5.0
pkgver=${_pkgver//-/}
pkgrel=3
pkgdesc='Lightweight multi-platform, multi-architecture disassembly framework (mingw-w64)'
url='https://www.capstone-engine.org/index.html'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-wheel")
source=("${_realname}-${_pkgver}.tar.gz::https://github.com/capstone-engine/capstone/archive/refs/tags/${pkgver}.tar.gz"
        "mingw_python.patch")
sha512sums=('e6794a9a06ed93edff9ae0b2c085d34010dd415e128ce4018b584fe35fa81182431f8fd6549d6a3c7a8a3bcca0691157c7a1c96971b7f51774960fd9afef9b8f'
            'dddeb10567a8d7d583c3eb40b04185898a7250df04e53f54f6e00038284ede665101500605de86e0801daf88c79042e76e327368a575a0ee6f50c437fc6b8596')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/mingw_python.patch"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # Does not define CAPSTONE_EXPORT for static library (#17779)
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -S "${_realname}-${_pkgver}" \
      -B "build-${MSYSTEM}-static"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -S "${_realname}-${_pkgver}" \
      -B "build-${MSYSTEM}-shared"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}-shared"
}

package_capstone() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}-shared"

  install -Dm644 "${srcdir}/${_realname}-${_pkgver}/docs/README" -t "${pkgdir}/${MINGW_PREFIX}/share/doc/${_realname}"
  install -Dm644 "${srcdir}/${_realname}-${_pkgver}/LICENSE.TXT" -t "${pkgdir}/${MINGW_PREFIX}/share/licenses/${_realname}"
}

package_python-capstone() {
  depends=("${MINGW_PACKAGE_PREFIX}-capstone"
           "${MINGW_PACKAGE_PREFIX}-python")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")

  cd "${_realname}-${_pkgver}/bindings/python"

  LIBCAPSTONE_PATH="${srcdir}/build-${MSYSTEM}-shared/libcapstone.dll" \
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation

  LIBCAPSTONE_PATH="${srcdir}/build-${MSYSTEM}-shared/libcapstone.dll" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 "${srcdir}/${_realname}-${_pkgver}/LICENSE.TXT" -t "${pkgdir}/${MINGW_PREFIX}/share/licenses/python-${_realname}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
